/*
* pps_bom
*/
package com.pitop.bom.ctrl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.pitop.account.entity.Account;
import com.pitop.base.ctrl.BaseCtrl;
import com.pitop.bom.entity.BomDetail;
import com.pitop.bom.service.BomDetailSVImpl;
import com.pitop.bom.vo.BomDetailVO;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.setting.service.SettingSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * bom明细 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/bomDetail")
@Slf4j
@Api(value = "bom明细控制器", description = "bom明细控制器")
public class BomDetailCtrl  extends BaseCtrl{

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private BomDetailSVImpl bomDetailSVImpl;
    @Autowired
    private SettingSVImpl settingSVImpl;//设置


    /**
     * 查询BomDetail一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSVImpl.load(id, code);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 查询BomDetail一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        BomDetail bomDetail = bomDetailSVImpl.get(id, code);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }


    /**
     * 根据条件id查询BomDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        BomDetail bomDetail = bomDetailSVImpl.loadById(id);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 根据条件code查询BomDetail一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        BomDetail bomDetail = bomDetailSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 根据条件id查询BomDetail一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        BomDetail bomDetail = bomDetailSVImpl.getById(id);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 根据条件code查询BomDetail一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询BomDetail一个详情信息", notes = "查询BomDetail一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        BomDetail bomDetail = bomDetailSVImpl.getByCode(code);
        log.info(JSON.toJSONString(bomDetail));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 查询BomDetail信息集合
     *
     * @return 分页对象
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编号", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore BomDetail bomDetail, @ApiIgnore Page<BomDetail> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<BomDetail> bomDetails = bomDetailSVImpl.list(bomDetail, page.getCurPage(), page.getPageSize());
        int total = bomDetailSVImpl.count(bomDetail);
        page.setTotalRow(total);
        page.setVoList(bomDetails);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询BomDetail信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询BomDetail信息集合", notes = "查询BomDetail信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<BomDetail> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<BomDetail> bomDetails = bomDetailSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = bomDetailSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(bomDetails);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建BomDetail", notes = "创建BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
            @ApiImplicitParam(name = "materialAssemblyCode", value = "物料或组件编码：多个使用逗号给开", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型[物料 Material ,组件 Assembly]", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "用量", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "位置", paramType = "query"),
            @ApiImplicitParam(name = "material", value = "材料", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore BomDetail bomDetail, HttpServletRequest request) {
        if (StringUtils.isEmpty(bomDetail.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getMaterialAssemblyCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getType())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //获取登录信息
         Account account =  this.loadAccount(request);
        bomDetailSVImpl.save(bomDetail, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 创建BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建BomDetail", notes = "创建BomDetail")
    @PostMapping("/save")
    @ResponseBody
    public BeanRet save(@RequestBody String bomDetails, HttpServletRequest request) {
        if (bomDetails == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //获取登录信息
       // Account account =  this.loadAccount(request);
        List<BomDetailVO> bomDetailVOS = JSONArray.parseArray(bomDetails, BomDetailVO.class);
        bomDetailSVImpl.save(bomDetailVOS, "11","11");
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 修改BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改BomDetail", notes = "修改BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "零件名", paramType = "query"),
            @ApiImplicitParam(name = "num", value = "用量", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "位置", paramType = "query"),
            @ApiImplicitParam(name = "material", value = "材料", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore BomDetail bomDetail, HttpServletRequest request) {
        if (StringUtils.isEmpty(bomDetail.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bomDetail.getNum())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        // Account account =  this.loadAccount(request);
        bomDetailSVImpl.modify(bomDetail, "11", "11");
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bomDetail);
    }

    /**
     * 根据主键删除BomDetail
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除BomDetail", notes = "删除BomDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomDetailSVImpl.delete(id, code);
        return BeanRet.create(true, "删除BomDetail成功");
    }


    /**
     * 修改bom详情状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1032):Develop(开发) Stable(稳定) Stop(停用)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomDetailSVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

}
